package com.interview.javabasic.Sort;/*
@李子宁
Happy,happy everyday!
冲鸭！
*/

import java.util.Arrays;

public class BubbleSort {
    public static void main(String[] args) {
        BubbleSort bubbleSort = new BubbleSort();
        int arr[] = {3,9,-1,10,20};
        bubbleSort.Bubble(arr);

    }



    //将冒泡排序封装成一个方法
    public void Bubble(int[] arr){
        int tmp = 0;//临时变量
        boolean flag = false;
        for (int j = 0; j <  arr.length-1; j++) {
            for (int i = 0; i < arr.length - 1 - j; i++) {
                //如果前面的数比后面的数大，就交换
                if (arr[i]>arr[i+1]){
                    flag = true;
                    tmp = arr[i];
                    arr[i] = arr[i+1];
                    arr[i+1] = tmp;
                }
            }
            System.out.printf("第%d趟排序\n",j+1);
            System.out.println(Arrays.toString(arr));
            if (!flag){
                break;
            }else {
                flag = false;
            }
        }
    }
}
